package com.yj_app.qualitytest.activity.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.hu_sir.sweet_dialog_reversion.SweetAlertDialog;
import com.yj_app.qualitytest.R;
import com.yj_app.qualitytest.activity.home.homecontract.HomeContract;
import com.yj_app.qualitytest.activity.home.homepresenter.HomePresenter;
import com.yj_app.qualitytest.activity.login.LoginActivity;
import com.yj_app.qualitytest.activity.sccjcx.SccjCxActivity;
import com.yj_app.qualitytest.activity.score.ScoreActivity;
import com.yj_app.qualitytest.base.BaseActivity;
import com.yj_app.qualitytest.base.BaseMvpActivity;
import com.yj_app.qualitytest.base.QtApplication;
import com.yj_app.qualitytest.callback.SyncRes;
import com.yj_app.qualitytest.common.Config;
import com.yj_app.qualitytest.info.BcInfo;
import com.yj_app.qualitytest.info.CompanyInfo;
import com.yj_app.qualitytest.info.HdInfo;
import com.yj_app.qualitytest.info.Loginfo;
import com.yj_app.qualitytest.info.SubScoreInfo;
import com.yj_app.qualitytest.info.TeamInfo;
import com.yj_app.qualitytest.util.PreferencesManager;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Copyright (C), 2015-2019/9/12, 石家庄云捷软件科技有限公司
 *
 * @ProjectName: HomeActivity.java
 * @Package: com.yj_app.qualitytest.activity.home
 * @ClassName: HomeActivity
 * @Description: TODO
 * @Author: Hu_Sir
 * @CreateDate: 2019/9/12--11:11
 * @UpdateUser: ?
 * @UpdateDate: 2019/9/12--11:11
 * @UpdateRemark: todo
 */
public class HomeActivity extends BaseMvpActivity<HomeContract.Presenter, HomeContract.View> implements HomeContract.View, SyncRes {

    @BindView(R.id.gm_tv)
    TextView mGmTv;
    @BindView(R.id.zf_tv)
    TextView mZfTv;
    @BindView(R.id.mw_tv)
    TextView mWfTv;
    @BindView(R.id.cx_tv)
    TextView mCxTv;
    @BindView(R.id.bb_tv)
    TextView mBbTv;
    @BindView(R.id.user_name)
    TextView mUserName;
    @BindView(R.id.ver_tv)
    TextView mVerTv;
    @BindView(R.id.btn_zx)
    Button mBtnZx;
    @BindView(R.id.btn_xgmm)
    Button mBtnXgmm;
    Loginfo loginfo;
    CompanyInfo companyInfo;
    boolean snyes = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        setTitleWithBack("功能模块选择");
        initView();
        //判断有没有缓存数据有的话质检
        if (hasSj()) {
            initData();
        } else {
            // TODO: 2019-11-06  
            snyes=true;
        }
    }

    private void initData() {
        presenter.getData(String.valueOf(Config.METHOD_BASIC_STANDARDCLASS), companyInfo.getCorp(), this);
        presenter.SynQStandard(String.valueOf(Config.METHOD_BASIC_STANDARD), companyInfo.getCorp(), this);
        presenter.syncLines(String.valueOf(Config.METHOD_BASIC_LINES), companyInfo.getCorp(), this);
        presenter.syncChejian(String.valueOf(Config.METHOD_BASIC_WORKSHOP), companyInfo.getCorp(), this);
        //班组
//        presenter.syncTeam(String.valueOf(Config.METHOD_BASIC_WORKSHOP), companyInfo.getCorp(), this);

        presenter.syncHeadBoy(String.valueOf(Config.METHOD_BASIC_HEAD), companyInfo.getCorp(), this);
        presenter.syncWorker(String.valueOf(Config.METHOD_BASIC_WORKER), companyInfo.getCorp(), this);

        presenter.syncQrelationship(String.valueOf(Config.METHOD_BASIC_RELATIONSHIP), companyInfo.getCorp(), this);


//        presenter.getData("QStandardClass", companyInfo.getCorp(), this);
//        presenter.SynQStandard("QStandard", companyInfo.getCorp(), this);
//        presenter.syncLines("QLines", companyInfo.getCorp(), this);
//        presenter.syncChejian("QWorkshop", companyInfo.getCorp(), this);
//        presenter.syncTeam("QTeam", companyInfo.getCorp(), this);
//        presenter.syncHeadBoy("QHead", companyInfo.getCorp(), this);
//        presenter.syncWorker("QWorker", companyInfo.getCorp(), this);
//        presenter.syncQrelationship("Qrelationship", companyInfo.getCorp(), this);
//        presenter.test("Qrelationship",companyInfo.getCorp());
    }

    @Override
    public HomeContract.Presenter initPresenter() {
        return new HomePresenter();
    }

    private void initView() {
        loginfo = PreferencesManager.getInstance(this).get(Config.LOGINFO, Loginfo.class);
        companyInfo = PreferencesManager.getInstance(this).get(Config.COMPANY_INFO, CompanyInfo.class);
        if (loginfo != null) {
            mUserName.setText("当前用户:" + loginfo.getNickname());
            if (TextUtils.equals(loginfo.getCode(), "sccj")) {
                mGmTv.setVisibility(View.GONE);
                mZfTv.setVisibility(View.GONE);
                mWfTv.setVisibility(View.GONE);
                mBtnXgmm.setVisibility(View.GONE);
            } else {
                mCxTv.setVisibility(View.GONE);
            }
        }
    }

    @OnClick({R.id.gm_tv, R.id.zf_tv, R.id.cx_tv, R.id.bb_tv, R.id.btn_zx, R.id.btn_xgmm, R.id.mw_tv})
    public void onClick(View v) {

        if (!snyes) {
            toastError("同步基本档案失败，请稍后重试");
            return;
        }


        Bundle bundle = new Bundle();
        switch (v.getId()) {
            default:
                break;
            case R.id.mw_tv:
                bundle.putInt(Config.FLAG, 3);
                bundle.putString(Config.P_ID, "03");
                selectTeam(bundle);
                break;
            case R.id.gm_tv:
                bundle.putInt(Config.FLAG, 1);
                bundle.putString(Config.P_ID, "01");
                selectTeam(bundle);
                break;
            case R.id.zf_tv:
                bundle.putInt(Config.FLAG, 2);
                bundle.putString(Config.P_ID, "02");
                selectTeam(bundle);
                break;
            case R.id.cx_tv:

                Intent intent = getIntent();
                intent.putExtras(bundle);
                intent.setClass(this, SccjCxActivity.class);
                startActivity(intent);


                break;
            case R.id.bb_tv:
                break;
            case R.id.btn_zx:
                new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE).setTitleText("确定要注销登录吗？")
                        .setConfirmText("确定").setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismissWithAnimation();
                        PreferencesManager.getInstance(HomeActivity.this).remove(Config.LOGINFO);
                        new SweetAlertDialog(HomeActivity.this, SweetAlertDialog.SUCCESS_TYPE).setTitleText("注销成功")
                                .setConfirmText("已注销").setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                startActivity(LoginActivity.class);
                                finish();
                            }
                        }).show();


                    }
                }).setCancelText("取消").show();
                break;
            case R.id.btn_xgmm:
                break;
        }
    }

    private void selectTeam(Bundle bundle) {
        View view = View.inflate(this, R.layout.layout_spinner, null);
        Spinner spinner = view.findViewById(R.id.spinner);
        List<TeamInfo> teamInfoList = LitePal.order("id").find(TeamInfo.class);
        List<String> list = new ArrayList<>();
        for (TeamInfo teamInfo : teamInfoList) {
            list.add(teamInfo.getGcode());
        }
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
        spinner.setAdapter(arrayAdapter);
        SweetAlertDialog sweetAlertDialog = new SweetAlertDialog(this, SweetAlertDialog.CONTENTVIEW_TYPE)
                .setTitleText("请选择班次").setmContentView(view).setConfirmText("确定").setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        ((ViewGroup) view.getParent()).removeView(view);
                        bundle.putSerializable(Config.TEAM_INFO, teamInfoList.get(spinner.getSelectedItemPosition()));
                        bundle.putInt(Config.TEAM_POSTION, spinner.getSelectedItemPosition());
                        startActivity(ScoreActivity.class, bundle);
                    }
                }).setCancelText("取消");
        sweetAlertDialog.show();


    }


    @Override
    public void syneSuccess(int count) {
        Log.e("count==", count + "");
        if (count == 7) {
            snyes = true;
            toastSuccess("同步完成");
            LitePal.deleteAll(TeamInfo.class);
            TeamInfo teamInfo = new TeamInfo();
            teamInfo.setGcode("一班");
            teamInfo.setGname("一班");
            TeamInfo teamInfo2 = new TeamInfo();
            teamInfo2.setGcode("二班");
            teamInfo2.setGname("二班");
            TeamInfo teamInfo3 = new TeamInfo();
            teamInfo3.setGcode("三班");
            teamInfo3.setGname("三班");
            teamInfo.save();
            teamInfo2.save();
            teamInfo3.save();


            //todo 完成同步班组信息
        }
    }


    long mExitTime;

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            toastWaring("再按一次退出" + getResources().getString(R.string.app_name));
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }


    @Override
    public void leftBack() {
        if (!hasSj()) {
            toastInfo("请先上传数据后再退出");
            return;
        }
        exit();
    }

    private boolean hasSj() {
        int count = LitePal.count(SubScoreInfo.class);
        int count2 = LitePal.count(HdInfo.class);
        int count3 = LitePal.count(BcInfo.class);

        if (count == 0 && count2 == 0 && count3 == 0) {
            return true;
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        if (!hasSj()) {
            toastInfo("请先上传数据后再退出");
            return;
        }
        finish();

    }
}
